package blackjack;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class Game {
	Deck deck = new Deck(4);
	ArrayList<Player> players = new ArrayList<Player>();
	static Scanner in;

	Random random = new Random();
	String[] names = new String[] { "Robert", "John", "Steve", "Jim", "Claire",
			"Bob", "Tom", "Dick", "Harry", "Elizabeth", "Susy", "Jessica",
			"Sarah", "Kyle", "Sally" };

	public Game(int numOfPlayers) {
		deck.shuffle();
		players.add(new Dealer(new Hand(deck.hit(), deck.hit()), 50, "Dealer",
				deck));
		players.add(new Player(new Hand(deck.hit(), deck.hit()), 50, "Player",
				deck));

		for (int i = 1; i < numOfPlayers; i++) {
			players.add(new AIPlayer(new Hand(deck.hit(), deck.hit()), 50,
					names[random.nextInt(names.length)], deck));

		}
	}

	public Game() {
		deck.shuffle();
		players.add(new Dealer(new Hand(deck.hit(), deck.hit()), 50, "Dealer",
				deck));
		players.add(new Player(new Hand(deck.hit(), deck.hit()), 50, "Player",
				deck));
	}

	void printPlayers() {
		for (int i = 0; i < players.size(); i++)
			System.out.println(players.get(i));
	}

	void play() {

		{				printPlayers();
// players.get(1).play();
			for (int i = 0; i < players.size(); i++) {
				while (!players.get(i).isBusted()
						&& !players.get(i).isSticking()) {
					players.get(i).play();
				}

			}
			printPlayers();

		}
	}

	void printDeck() {
		System.out.println(deck);
		deck.printCardValues();
	}

	public static void main(String[] args) {
		in = new Scanner(System.in);
		boolean play = true;
		char input;
		String i;
		while (play) {
		System.out.println("Would you like to play? [Y]es or [N]o");
		 i = in.next().toUpperCase();
		 input = i.charAt(0);
		
			switch (input) {
			case 'Y':
				System.out.println("How many players would you like to have?");
				int numPlayers = in.nextInt();
				Game game = new Game(numPlayers);
				game.play();
				break;
			case 'N':
				System.out.println("Okay.  Maybe next time!");
				play = false;
				break;
			default:
				break;
			}
			// game.printPlayers();
			// game.printDeck();
			// game.play();
			// game.printPlayers();
			// game.printDeck();

		}

		// private static void InitializeDeck() {

	}
}